﻿using System;
using System.IO;
using doufu.Common.Extensen;

namespace doufu.Common
{
    public class LoadFile
    {
        static public void Load(Action<StreamReader> func, string filePath)
        {
            var table = File.OpenText(filePath);
            func(table);
            table.Close();
            table.Dispose();
        }

        static public void Load<T>(Action<T> func, string filePath)
        {
            var table = File.OpenText(filePath);
            var text = table.ReadToEnd();
            func(text.FromJson<T>());
            table.Close();
            table.Dispose();
        }

        static public string Load(string filePath)
        {
            var table = File.OpenText(filePath);
            var text = table.ReadToEnd();
            table.Close();
            table.Dispose();
            return text;
        }

        static public T Load<T>(string filePath)
        {
            var table = File.OpenText(filePath);
            var text = table.ReadToEnd();
            var t = text.FromJson<T>();
            table.Close();
            table.Dispose();
            return t;
        }
    }
}